package com.morgankelly.dao;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.morgankelly.po.Account;
import com.morgankelly.po.Blog;
import com.morgankelly.po.BlogComment;
import com.morgankelly.po.Category;
import com.morgankelly.util.Constants;
import com.morgankelly.util.PMF;

public class BlogDAO extends GenericDAO<Blog> {

	private static BlogDAO instance = null;

	private BlogDAO() {}

	public static synchronized BlogDAO get() {
		if (instance == null) {instance = new BlogDAO();}
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	public List<Blog> getPopularBlogList() {
		
		//int topNum = Constants.SIDE_BAR_TAB_LENGTH;
		
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(Blog.class);
		List<Blog> blogList= (List<Blog>) query.execute();

		return  blogList;
	}
	
	@SuppressWarnings("unchecked")
	public List<Blog> getBlogListBySearchKey(String searchKey) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(Blog.class);
		List<Blog> blogList;

		if (searchKey !=null && !searchKey.equals("")) {
			//TODO:
			blogList = (List<Blog>) query.execute();
		} else {
			blogList = (List<Blog>) query.execute();
		}
		return  blogList;
	}

	public int getBlogNumByCategoryID(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Key keyObj = KeyFactory.createKey(Category.class.getSimpleName(), id);

		Query query = pm.newQuery("select count(id) from " 
				+ Blog.class.getName() 
				+ " where categoryKey == keyObj");
		query.declareParameters("com.google.appengine.api.datastore.Key keyObj");

		return Integer.parseInt(query.execute(keyObj).toString());
	}
	
	public void add(Blog blog) {

		Key accountKey = KeyFactory.createKey(Account.class.getSimpleName(), blog.getAccountID());
		Key categoryKey = KeyFactory.createKey(Category.class.getSimpleName(), blog.getCategoryID());

		Calendar current = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_TIME_5);

		blog.setCreateTime(simpleDateFormat.format(current.getTime()));
		blog.setAccountKey(accountKey);
		blog.setCategoryKey(categoryKey);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.makePersistent(blog);
	}
	
	public void update(Long id, String title, String content, Long accountID,
			Long categoryID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Blog entry = pm.getObjectById(Blog.class, id);

		entry.setTitle(title);
		entry.setContent(content);

		Key accountKey = KeyFactory.createKey(Account.class.getSimpleName(), accountID);
		Key categoryKey = KeyFactory.createKey(Category.class.getSimpleName(), categoryID);

		entry.setAccountKey(accountKey);
		entry.setCategoryKey(categoryKey);

		pm.makePersistent(entry);

	}
	
	public void delete(Blog blog) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.deletePersistent(blog);
		
		Query query = pm.newQuery("delete from " 
				+ BlogComment.class.getName() 
				+ " where blogKey == keyObj");
		query.declareParameters("com.google.appengine.api.datastore.Key keyObj");

		query.execute(blog.getKey());
	}
	
}